<div class="grid_12">
    <?php if ($this->session->flashdata('variation_msg') != ''): ?>
        <ul class="message success">
            <li><?php echo $this->session->flashdata('variation_msg') ?></li>
        </ul>
    <?php endif; ?>
    <a href="<?php echo site_url('admin/variation/add') ?>" class="checker" style="padding:3px 6px;margin-bottom: 7px">Thêm sim mới</a>
    <a href="<?php echo site_url('admin/variation/import') ?>" class="checker" style="padding:3px 6px;margin-bottom: 7px">Nhập sim từ excel</a>
    <br/>
    <div class="searchWrap" style="margin-bottom: 5px;float:left">
        <select name="filterGroup" class="filter" id="filterGroup">
            <option value="" <?php echo ($fGroup == '') ? 'selected="selected"' : '' ?>>Tất cả các nhóm</option>
            <?php foreach($group as $g): ?>
                <option value="<?php echo $g['id'] ?>" <?php echo ($fGroup == $g['id']) ? 'selected="selected"' : '' ?>><?php echo $g['name'] ?></option>
            <?php endforeach; ?>
        </select>
        <select name="filterNetwork" class="filter" id="filterNetwork">
            <option value="" <?php echo ($fNetwork == '') ? 'selected="selected"' : '' ?>>Tất cả các nhà mạng</option>
            <?php foreach($network as $n): ?>
                <option value="<?php echo $n['id'] ?>" <?php echo ($fNetwork == $n['id']) ? 'selected="selected"' : '' ?>><?php echo $n['name'] ?></option>
            <?php endforeach; ?>
        </select>
        <select name="filterAgent" class="filter" id="filterAgent">
            <option value="" <?php echo ($fAgent == '') ? 'selected="selected"' : '' ?>>Tất cả các đại lý</option>
            <?php foreach($city as $c): ?>
                <option value="<?php echo $c['id'] ?>" <?php echo ($fAgent == $c['id']) ? 'selected="selected"' : '' ?>><?php echo $c['name'] ?></option>
            <?php endforeach; ?>
        </select>
        <select name="filterStatus" class="filter" id="filterStatus">
            <option value="" <?php echo ($status == '') ? 'selected="selected"' : '' ?>>Tất cả trạng thái</option>
            <option value="0" <?php echo ($status == '0') ? 'selected="selected"' : '' ?>>Hiển thị</option>
            <option value="1" <?php echo ($status == '1') ? 'selected="selected"' : '' ?>>Ẩn</option>
        </select>
        <select name="filterOrdered" class="filter" id="filterOrdered">
            <option value="" <?php echo ($order == '') ? 'selected="selected"' : '' ?>>Tất cả tình trạng</option>
            <option value="0" <?php echo ($order == '0') ? 'selected="selected"' : '' ?>>Đã bán</option>
            <option value="1" <?php echo ($order == '1') ? 'selected="selected"' : '' ?>>Còn hàng</option>
        </select>
        <input type="text" id="keyword" name="keyword" class="keyword" value="<?php echo $keyword ?>" placeholder="Nhập từ khóa tìm kiếm"/>
        <input type="button" id="searchVariation" value="Tìm kiếm" rel="<?php echo site_url('admin/variation/index') ?>"/>
        <select name="filterNumber" class="filter" id="filterNumber">
            <option value="50" <?php echo ($number == 50) ? 'selected="selected"' : '' ?>>50 kết quả/trang</option>
            <option value="100" <?php echo ($number == 100) ? 'selected="selected"' : '' ?>>100 kết quả/trang</option>
            <option value="200" <?php echo ($number == 200) ? 'selected="selected"' : '' ?>>200 kết quả/trang</option>
            <option value="300" <?php echo ($number == 300) ? 'selected="selected"' : '' ?>>300 kết quả/trang</option>
            <option value="400" <?php echo ($number == 400) ? 'selected="selected"' : '' ?>>400 kết quả/trang</option>
            <option value="500" <?php echo ($number == 500) ? 'selected="selected"' : '' ?>>500 kết quả/trang</option>
        </select>
        <a href="javascript:;" class="excel" id="excel">Excel</a>
    </div>
    <form name="variationAct" method="POST" id="variationAct" action="<?php echo site_url('admin/variation/actionSelected') ?>">
        <br class="clear"/>
    <div class="action">
        <p>
            Các mục đã chọn:
            <select name="actionSlc" id="actionSlc">
                <option value="" selected="selected">----</option>
                <option value="delete">Xóa</option>
                <option value="hide">Ẩn</option>
                <option value="show">Hiện</option>
            </select>
            <a href="javascript:;" onclick="actionSubmit()" class="checker" id="actSlc" style="padding:4px 6px">Xác nhận</a>
        </p>
    </div>
    <div id="demo" class="clearfix">
        <table class="display" id="dataTable"> 
            <thead> 
                <tr> 
                    <th class="center" style="padding:3px 10px">
                        <input type="checkbox" id="chkAll" onclick="chkVariationChange();" />
                    </th>
                    <th>Số điện thoại</th> 
                    <th>Mạng</th> 
                    <th>Nhóm</th> 
                    <th>Đại lý</th> 
                    <th>Giá tiền</th> 
                    <th>Trạng thái</th> 
                    <th>Tình trạng</th> 
                    <th>Action</th>
                </tr> 
            </thead>
            <tbody>
            <?php if(is_array($sim)) {?>
                <?php foreach($sim as $s): ?>
                    <tr class="<?php echo ($s['delete_flag'] == 0) ? 'gradeA' : 'gradeX' ?>"> 
                        <td class="center">
                            <input type="checkbox" class="variationChk" name="variation[]" value="<?php echo $s['id'];?>" />
                        </td>
                        <td class="center"><?php echo $s['phone'] ?></td> 
                        <td class="center"><?php echo $s['network_name'] ?></td>
                        <td class="center"><?php echo $s['group_name'] ?></td>
                        <td class="center"><?php echo $s['city_name'] ?></td>
                        <td class="center"><span style="color: red"><?php echo ($s['price'] == $s['price_update'])? number_format($s['price'], 0, '.', '.'): number_format($s['price_update'], 0 ,'.','.') ;?> VNĐ</span></td>
                        <td class="center">
                            <?php 
                                switch ($s['delete_flag']) {
                                    case 0 : 
                                        echo 'Hiển thị';
                                        break;
                                    case 1 :
                                        echo 'Ẩn';
                                        break;
                                }
                            ?>
                        </td> 
                        <td class="center">
                            <?php 
                                switch ($s['status']) {
                                    case 0 : 
                                        echo 'Đã bán';
                                        break;
                                    case 1 :
                                        echo 'Còn hàng';
                                        break;
                                }
                            ?>
                        </td> 
                        <td class="center">
                            <a href="<?php echo site_url('admin/variation/edit/' . $s['id']) ?>"><img src ="<?php echo STATIC_DOMAIN?>/images/simsodep/edit.png" alt="edit" title="Sửa"></a> 
                            -
                            <?php if($s['delete_flag'] == 0): ?>
                            <a id="icon_hide" href="<?php echo site_url('admin/variation/hide/'. $s['id']) ?>"><img src ="<?php echo STATIC_DOMAIN?>/images/simsodep/hide.png" width="18" height="18" alt="hide" title="Ẩn"></a>
                            <?php else: ?>
                            <a id="icon_show" href="<?php echo site_url('admin/variation/show/'. $s['id']) ?>"><img src ="<?php echo STATIC_DOMAIN?>/images/simsodep/show.png" width="18" height="18" alt="hide" title="Hiện"></a> 
                            <?php endif; ?>
                            - 
                            <?php if($s['status'] == 0): ?>
                            <a id="icon_new" href="<?php echo site_url('admin/variation/StatusNew/'. $s['id']) ?>"><img src ="<?php echo STATIC_DOMAIN?>/images/simsodep/stocking.png" width="18" height="18" alt="hide" title="Còn hàng"></a>
                            <?php else: ?>
                            <a id="icon_sale" href="<?php echo site_url('admin/variation/Sale/'. $s['id']) ?>"><img src ="<?php echo STATIC_DOMAIN?>/images/simsodep/sale.png" width="18" height="18" alt="hide" title="Đã bán"></a> 
                            <?php endif; ?>
                            - 
                            
                            <a class="delete" href="<?php echo site_url('admin/variation/delete/'. $s['id']) ?>"><img src ="<?php echo STATIC_DOMAIN?>/images/simsodep/delete.png" alt="delete" title="Xóa"></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php }?>
            </tbody>
        </table>
        <!-- Begin pagination -->
        <div class="pagination" style="margin-top: 5px">
            <?php echo $pagination ?>
        </div>
        <!-- End pagination -->
    </div>
    </form>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        var filter = $('.filter');
        filter.change(function() {
            var filterStatus = $('#filterStatus').val(),
                filterOrdered = $("#filterOrdered").val(),
                filterGroup = $("#filterGroup").val(),
                filterNetwork = $("#filterNetwork").val(),
                filterAgent = $("#filterAgent").val(),
                filterNumber = $("#filterNumber").val(),
                keywork = $("#keyword").val();
            window.location = '<?php echo site_url('admin/variation/index') ?>?k=' + keywork + "&s=" + filterStatus + "&o=" + filterOrdered + "&g=" + filterGroup +"&n=" + filterNetwork + "&a=" + filterAgent + "&num=" + filterNumber;
        });
        
        $("#excel").click(function(e) {
            var filterStatus = $('#filterStatus').val(),
                filterOrdered = $("#filterOrdered").val(),
                filterGroup = $("#filterGroup").val(),
                filterNetwork = $("#filterNetwork").val(),
                filterAgent = $("#filterAgent").val(),
                filterNumber = $("#filterNumber").val(),
                keywork = $("#keyword").val(),
                url = '<?php echo site_url('admin/variation/export') ?>?type=1&k=' + keywork + "&s=" + filterStatus + "&o=" + filterOrdered + "&g=" + filterGroup +"&n=" + filterNetwork + "&a=" + filterAgent + "&num=" + filterNumber + "&page=<?php echo $curpage ?>";
                window.location = url;
            return true;
        });
        
        $("#searchVariation").click(function() {
            var filterStatus = $('#filterStatus').val(),
                filterOrdered = $("#filterOrdered").val(),
                filterGroup = $("#filterGroup").val(),
                filterNetwork = $("#filterNetwork").val(),
                filterAgent = $("#filterAgent").val(),
                filterNumber = $("#filterNumber").val(),
                keywork = $("#keyword").val();
            window.location = '<?php echo site_url('admin/variation/index') ?>?k=' + keywork + "&s=" + filterStatus + "&o=" + filterOrdered + "&g=" + filterGroup +"&n=" + filterNetwork + "&a=" + filterAgent + "&num=" + filterNumber;
        });
        
        $(".variationChk").click(function() {
            var input = $('input:checkbox[name="variation[]"]'),
                chkAll = $('#chkAll');
            if(input.length == input.filter(':checked').length) {
                chkAll.attr('checked', true);
                chkAll.parent().addClass('checked').removeClass('active');
            } else {
                chkAll.attr('checked', false);
                chkAll.parent().removeClass('active').removeClass('checked');
            }
        });
    });
    
    function chkVariationChange()
	{
		var _value = $('#chkAll:checked').val() == 'on' ? true : false;
		$('input:checkbox[name="variation[]"]').each(function(index){
            $(this).attr('checked', _value);
            $(this).trigger('click');
			$(this).attr('checked', _value);
		});
	}
    
    function actionSubmit() {
        var slc = $("#actionSlc"),
            iptChk = $('input:checkbox[name="variation[]"]');
        if(slc.val() == '') {
            alert('Bạn cần chọn một hành động với các sim đã chọn!');
            return false;
        }
        
        if(iptChk.filter(':checked').length == 0) {
            alert('Bạn cần chọn ít nhất 1 sim!');
            return false;
        }
        
        document.variationAct.submit();
    }
</script>